﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// RubbishInfo 的摘要说明
/// </summary>
/// 
using DeviceInfoManage.DataAccessHelper;
using DeviceInfoManage.DataAccessLayer;

namespace DeviceInfoManage.BusinessLogicLayer
{
    /*本业务类对应数据库中的rubbishInfo表，它处理设备报废业务*/
    /*设备报废信息表
    CREATE TABLE [dbo].[rubbishInfo] (
	  [deviceId] [int] NOT NULL ,						                        //报废设备编号
	  [deviceRubbishTime] [datetime] NULL ,					                    //报废日期
	  [deviceRubbishReason] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,	//报废原因
	  [deprecitionMoney] [float] NULL 					                        //折旧金额
    ) ON [PRIMARY]*/
    public class RubbishInfo
    {
        private int deviceId; /*报废的设备编号*/
        private DateTime deviceRubbishTime;/*报废日期*/
        private string deviceRubbishReason;/*报废原因*/
        private float deprecitionMoney;/*折旧金额*/
        private string errMessage; /*业务操作错误信息*/
        #region MyRegion
        public int DeviceId
        {
            set { this.deviceId = value; }
            get { return this.deviceId; }
        }
        public DateTime DeviceRubbishTime
        {
            set { this.deviceRubbishTime = value; }
            get { return this.deviceRubbishTime; }
        }
        public string DeviceRubbishReason
        {
            set { this.deviceRubbishReason = value; }
            get { return this.deviceRubbishReason; }
        }
        public float DeprecitionMoney
        {
            set { this.deprecitionMoney = value; }
            get { return this.deprecitionMoney; }
        }
        public string ErrMessage
        {
            set { this.errMessage = value; }
            get { return this.errMessage; }
        }
        #endregion
        /*构造函数初始化*/
        public RubbishInfo()
        {
            deviceId = 0;
            deviceRubbishTime = DateTime.MinValue;
            deviceRubbishReason = "";
            deprecitionMoney = 0.0f;
            errMessage = "";
        }
        /*加入设备报废信息*/
        public bool AddDeviceRubbishInfo()
        {
            /*判断是否已经选择了报废的设备*/
            if (this.deviceId == 0)
            {
                this.errMessage = "你没有选择报废的设备";
                return false;
            }
            /*判断该设备在系统中是否已经存在*/
            if (!(new DeviceInfo()).IsExist(deviceId))
            {
                this.errMessage = "不存在该设备编号的设备信息";
                return false;
            }
            /*检查设备状态*/
            if ((new DeviceInfo()).GetDeviceState(deviceId) != 1)
            {
                this.errMessage = "该设备未处于空闲状态，不能报废!";
                return false;
            }
            /*通过验证后，可以执行设备报废信息的添加了*/
            string insertString = "insert into rubbishInfo(deviceId,deviceRubbishTime,deviceRubbishReason,deprecitionMoney) values (";
            insertString += deviceId + ",'";
            insertString += deviceRubbishTime + "',";
            insertString += SqlString.GetQuotedString(deviceRubbishReason) + ",";
            insertString += deprecitionMoney + ")";
            /*设备报废后要修改该设备的状态为报废状态,系统中报废状态用数字4表示的*/
            string updateString = "update deviceInfo set deviceState=4 where deviceId=" + deviceId;
            string[] sqlStrings = new string[] { insertString, updateString };
            DataBase db = new DataBase();
            /*调用数据层的ExecuteSQL执行批量的sql产生的事务操作，成功返回true*/
            if (db.ExecuteSQL(sqlStrings) == false)
            {
                this.errMessage = "设备报废进行数据库操作时发生了错误!";
                return false;
            }
            return true;
        }

        /*根据条件查询设备报废信息*/
        public DataSet QueryRubbishInfo(string deviceName, string deviceType, string startTime, string endTime)
        {
            string queryString = "select * from rubbishInfoView where deviceName like '%" + deviceName;
            queryString += "%' and deviceTypeName like '%" + deviceType + "%'";
            /*如果用户设置了外借开始时间查询条件*/
            if (startTime != "")
                queryString += " and deviceRubbishTime > '" + Convert.ToDateTime(startTime) + "'";
            /*如果用户设置了外借结束时间查询条件*/
            if (endTime != "")
                queryString += " and deviceRubbishTime <'" + Convert.ToDateTime(endTime) + "'";
            DataBase db = new DataBase();
            return db.GetDataSet(queryString);
        }

        /*清除系统中已经报废了的设备的所有信息,报废的设备的设备状态用数字4表示的*/
        public bool ClearRubbishInfo()
        {
            /*删除报废设备信息的sql语句*/
            string deleteRubbishInfoString = "delete from rubbishInfo";
            /*删除报废设备的借还信息的sql语句*/
            string deleteRubbishLendInfoString = "delete from lendInfo where deviceId in(select deviceId from deviceInfo where deviceState=4)";
            /*删除报废设备的维修信息的sql语句*/
            string deleteRubbishRepairInfoString = "delete from repairInfo where deviceId in(select deviceId from deviceInfo where deviceState=4)";
            /*最后执行报废设备的sql语句*/
            string deleteDeviceInfoString = "delete from deviceInfo where deviceState = 4";
            /*将上面四个sql语句组成一个事务操作提交给数据层处理*/
            string[] deleteStrings = new string[] { deleteRubbishInfoString,deleteRubbishLendInfoString,deleteRubbishRepairInfoString,deleteDeviceInfoString };
            DataBase db = new DataBase();
            if (!db.ExecuteSQL(deleteStrings))
                return false;
            return true;
        }
    }

}
